#ifndef __GLF_CORE_MD5_INCLUDED__
#define __GLF_CORE_MD5_INCLUDED__

namespace glf {

struct MD5 {
	typedef unsigned int size_type; // must be 32bit
	typedef unsigned char uint1; //  8bit
	typedef unsigned int uint4;  // 32bit

	MD5();
	MD5(const std::string& text);
	void update(const unsigned char *buf, size_type length);
	void update(const char *buf, size_type length);
	void finalize();
	std::string hexdigest() const;
	friend std::ostream& operator<<(std::ostream&, MD5 md5);


private:
	void init();
	enum {blocksize = 64}; // VC6 won't eat a const static int here

	void transform(const uint1 block[blocksize]);
	static void decode(uint4 output[], const uint1 input[], size_type len);
	static void encode(uint1 output[], const uint4 input[], size_type len);

	bool finalized;
	uint1 buffer[blocksize]; // bytes that didn't fit in last 64 byte chunk
	uint4 count[2];   // 64bit counter for number of bits (lo, hi)
	uint4 state[4];   // digest so far
	uint1 digest[16]; // the result
};

std::string Md5(const std::string& str);

}

#endif